package top.oylan.manager.controller.mp;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.*;
import top.oylan.manager.entity.extend.BookCondition;
import top.oylan.manager.entity.extend.WatchedExtend;
import top.oylan.manager.entity.mp.MpResult;
import top.oylan.manager.service.ProductService;
import top.oylan.manager.service.UserService;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-08-11
 */
@RestController
@RequestMapping("/mp/user")
@CrossOrigin
public class MpUserController {

    @Resource
    UserService userService;

    @Resource
    ProductService productService;

    /**
     * 用户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public MpResult index(@RequestParam("id")Integer id){

        MpResult result = new MpResult();
        JSONObject data = new JSONObject();
        User user = userService.selectUserById(id);
        List<WatchedExtend> list = productService.selectMyWatched(id);
        data.put("userInfo",user);
        data.put("watched",list);
        result.setData(data);
        return result;
    }

    /**
     * 我的课程
     * @param id
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/course")
    public MpResult myCourse(@RequestParam("id")Integer id,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "size",defaultValue = "12")Integer size){

        MpResult<Object> result = new MpResult<>();
        Pager pager = new Pager();
        WatchedExtend condition = new WatchedExtend();
        condition.setUserId(id);
        condition.setType((byte)1);
        pager.setCondition(condition);
        pager.setEveyPageRow(size);
        pager.setCurrentPage(page);
        List<WatchedExtend> list = productService.selectMyCourse(pager);
        pager.setList(list);
        result.setData(pager);
        return result;
    }

    /**
     * 浏览记录
     * @param userId
     * @param productId
     * @param sectionId
     * @param offset
     * @return
     */
    @PostMapping("/watched")
    public MpResult watched(@RequestParam("userId")Integer userId,
                            @RequestParam("productId")Integer productId,
                            @RequestParam("sectionId")Integer sectionId,
                            @RequestParam("offset")String offset){
        Watched condition = new Watched();
        condition.setUserId(userId);
        condition.setProductId(productId);
        condition.setSectionId(sectionId);
        Watched watched = productService.findUserWatched(condition);
        if(watched == null){
            watched = new Watched();
            watched.setUserId(userId);
            watched.setProductId(productId);
            watched.setSectionId(sectionId);
            watched.setCreateTime(new Date());
        }
        watched.setOffset(offset);
        watched.setUpdateTime(new Date());
        int res = productService.saveWatched(watched);
        MpResult result = new MpResult();
        if(res == 0){
            result.setCode(0);
            result.setMsg("保存数据失败");
        }
        return result;
    }

    /**
     * 提交评价
     * @param comment
     * @return
     */
    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    public MpResult comment(@RequestBody Comment comment){
        MpResult result = new MpResult();
        comment.setCreateTime(new Date());
        int  res = userService.comment(comment);
        if (res == 0){
            result.setCode(0);
            result.setMsg("评价失败");
        }
        return result;
    }

    @RequestMapping("/books")
    public MpResult books(@RequestParam("userId")Integer userId,
                          @RequestParam(value = "page",defaultValue = "1")Integer page,
                          @RequestParam(value = "size",defaultValue = "12")Integer size,
                          @RequestParam(value = "regulation",defaultValue = "1")Byte regulation){
        MpResult result = new MpResult<>();
        Pager pager = new Pager();
        pager.setEveyPageRow(size);
        pager.setCurrentPage(page);
        BookCondition bookCondition = new BookCondition();
        bookCondition.setUserId(userId);
        bookCondition.setRegulation(regulation);
        pager.setCondition(bookCondition);
        List<Book> books = productService.selectMyBooksByPager(pager);
        pager.setList(books);
        result.setData(pager);
        return result;
    }

    /**
     * 点赞   -- 还差增减赞数
     * @param liked
     * @param userId
     * @return
     */
    @RequestMapping(value = "/like",method = RequestMethod.POST)
    public MpResult like(@RequestBody Liked liked,@RequestParam("userId")Integer userId){
        liked.setUserId(userId);
        userService.saveLiked(liked);
        return new MpResult();
    }

    /**
     * 取消 -- 还差增减赞数
     * @param liked
     * @param userId
     * @return
     */
    @RequestMapping("/like/cancel")
    public MpResult cancel(@RequestBody Liked liked,@RequestParam("userId")Integer userId){
        liked.setUserId(userId);
        userService.cancelLike(liked);
        return new MpResult();
    }
}
